package com.shoplink.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shoplink.launcher.R;
import com.shoplink.view.IPEditTextView.TextChangeListener;

public class IPSettingView extends LinearLayout implements
		OnFocusChangeListener {

	public IPSettingView(Context context) {
		super(context);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	public IPSettingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOrientation(LinearLayout.HORIZONTAL);
	}

	char[] nums = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			if (getChildAt(i) instanceof EditText) {
				IPEditTextView editText = ((IPEditTextView) getChildAt(i));

				editText.setOnKeyListener(new OnKeyListener() {

					@Override
					public boolean onKey(View v, int keyCode, KeyEvent event) {
						if (event.getAction() == KeyEvent.ACTION_DOWN
								&& keyCode == 66) {
							InputMethodManager imm = (InputMethodManager) v
									.getContext().getSystemService(
											Context.INPUT_METHOD_SERVICE);
							if (imm.isActive()) {
								imm.hideSoftInputFromWindow(
										v.getApplicationWindowToken(), 0);
							}
							return true;
						}

						return false;
					}
				});

				editText.setOnFocusChangeListener(new OnFocusChangeListener() {

					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						setEditTextSelection(v);
						IPSettingView.this.onFocusChange(v, hasFocus);
					}
				});
				editText.setTextChangeListener(new TextChangeListener() {

					@Override
					public void onTextChange(CharSequence s, int start,
							int before, IPEditTextView editText) {

						char[] text = s.toString().toCharArray();
						if (text.length == 4) {
							editText.setText(s.toString().substring(0, 3));
							return;
						}
						boolean isNum = false;
						boolean isBreak = false;
						for (int j = 0; j < text.length; j++) {
							if (isBreak) {
								break;
							}
							for (int k = 0; k < nums.length; k++) {
								if (text[j] == nums[k]) {
									isNum = true;
								}
								if (nums.length - 1 == k && !isNum) {
									isBreak = true;
									break;
								} else if (nums.length - 1 == k && isNum
										&& j < text.length - 1) {
									isNum = false;
								}
							}

						}

						if (!isNum) {
							if (!TextUtils.isEmpty(s.toString())) {
								editText.setText("");
								Toast.makeText(
										getContext(),
										getContext().getResources().getString(
												R.string.ip_enter_err), 1000)
										.show();
							}
							editText.requestFocus();
						} else {
							int num = Integer.valueOf(s.toString());
							if (num > 255) {
								Toast.makeText(
										getContext(),
										getContext().getResources().getString(
												R.string.ip_enter_err1), 1000)
										.show();
								editText.setText("");
								editText.requestFocus();
							} else if (s.toString().length() == 3) {
								int id = editText.getNextFocusRightId();
								View v = IPSettingView.this.findViewById(id);
								if (v != null) {
									v.requestFocus();
									setEditTextSelection(v);
								}
							}
						}

					}

				});
			}

		}

	}

	public void setEditTextSelection(View v) {
		IPEditTextView child = (IPEditTextView) v;
		String content = child.getText().toString();
		if (!TextUtils.isEmpty(content)) {
			child.setSelection(content.length());
		}
	}

	public String getText() {
		int count = getChildCount();
		String ips = "";
		for (int i = 0; i < count; i++) {
			if (getChildAt(i) instanceof IPEditTextView) {
				ips += ((IPEditTextView) getChildAt(i)).getText().toString();
			} else {
				ips += ((TextView) getChildAt(i)).getText().toString();
			}

		}
		return ips;
	}

	public void setText(String str) {
		if (!TextUtils.isEmpty(str)) {
			String[] ips = str.split("\\.");
			int index = 0;
			for (int i = 0; i < getChildCount(); i++) {
				if (getChildAt(i) instanceof IPEditTextView) {
					if (index < ips.length) {
						((IPEditTextView) getChildAt(i)).setText(ips[index]);
						index++;
					}
				}
			}
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			this.setBackgroundResource(R.drawable.input_focus);
		} else {
			this.setBackgroundResource(R.drawable.input_normal);
		}
	}

}
